<?php
namespace Org\Util;
/**
 * [LoadPage 缓存式分页设计]
 * @Author   :seven
 * @DateTime :2016-05-31T17:00:08+0800
 * @Detail   :
 */
class LoadPage {
    private $tab_name = "";
    private $condition;
    private $pageLoad;
    private $pageSize;

    /**
     * [page 调用获取数据包]
     * @Author   :seven
     * @DateTime :2016-05-31T17:03:18+0800
     * @Detail   :
     * @param    [integer]    $page      [所在页码]
     * @param    integer $pageLoad [缓存页面的数量]
     * @param    integer $pageSize [每页的大小]
     * @param    string $condition [查询条件]
     */
    public function __construct($Elem, $tab_name) {
        $this->condition = $Elem['condition'];
        $this->pageLoad = $Elem['pageLoad'] < 3 ? 3 : $Elem['pageLoad'];
        $this->pageSize = $Elem['pageSize'];

        $this->tab_name = $tab_name;
    }

    /**
     * [pageLimit 获取数据的长度]
     * @Author   :seven
     * @DateTime :2016-06-03T00:29:36+0800
     * @Detail   :
     * @param    [type]        $page     [当前页码]
     * @param    integer $pageLoad [缓存页面数量]
     * @param    integer $pageSize [页面大小]
     * @return   [type]    []
     */
    public function limit($page) {
        $LimitEND = $this->pageLoad * $this->pageSize;
        $LimitStart = Floor($page / $this->pageLoad) * $LimitEND;

        return array("start" => $LimitStart,
            "end" => $LimitEND);
    }

    /**
     * [page 单表查询]
     * @Author   :seven
     * @DateTime :2016-06-03T00:30:38+0800
     * @Detail   :
     * @param    [type]    $page      [当前页码]
     * @param    string $condition [查询条件]
     * @param    integer $pageLoad [缓存页面数量]
     * @param    integer $pageSize [页面大小]
     * @param    string $field [查询条目]
     */
    public function page($page, $field = '') {
        $db = M($this->tab_name);
        /*查询固定块*/
        $LimitEND = $this->pageLoad * $this->pageSize;
        $LimitStart = Floor($page / $this->pageLoad) * $LimitEND;

        /*获取数据*/
        $data = $db->field($field)->where($this->condition)->limit($LimitStart, $LimitEND)->select();
        /*统计页面总数*/
        $count = $db->field($field)->where($this->condition)->count();
        /*ceil进一取法*/
        $count = Ceil($count / $this->pageSize);

        $dataBox['pageSum'] = $count;
        //数据分包
        $dataBox['dataBox'] = $this->madeLoopDate($data);
        //测试使用
        // $dataBox['dataBox']['LimitStart'] = $LimitStart;
        // $dataBox['dataBox']['LimitEND'] = $LimitEND;
        return json_encode($dataBox);
    }

    /**
     * [madeLoopDate 数据处理]
     * @Author   :seven
     * @DateTime :2016-06-03T00:32:04+0800
     * @Detail   :
     * @param    [type]    $data [description]
     * @param    [type]    $size [description]
     * @param    [type]    $loop [description]
     * @return   [type]    [description]
     */
    private function madeLoopDate($data) {
        $loopDate = array();
        for ($i = 0; $i < $this->pageLoad; $i++) {
            for ($j = 0; $j < $this->pageSize; $j++) {
                if (!empty($data[$this->pageSize * $i + $j])) {
                    $loopDate[$i][$j] = $data[$this->pageSize * $i + $j];
                }
            }
        }
        return $loopDate;
    }

    public function getRes($data, $count) {
        $res = array();
        $res['dataBox'] = $this->madeLoopDate($data);
        $res['pageSum'] = Ceil($count / $this->pageSize);

        return json_encode($res);
    }

    /**
     * [show 页码HTML]
     * @Author   :seven
     * @DateTime :2016-06-03T00:32:39+0800
     * @Detail   :
     * @return   [type]                    [description]
     */
    static public function show() {
        $lang = C('WEB_LANG');
        if (empty($lang) || $lang == 'zh') {
            return '<div id="pageLoad">
	        <a href="javascript:click_First_End(0)" class="click_btn" title="First Page">第一页</a>
	        <button id="prevPage" class="click_btn">上一页</button>
			<span id="number_span">
			</span>
	        <button id="nextPage" class="click_btn">下一页</button>
	        <a href="javascript:click_First_End(1)" class="click_btn">最后一页</a>
	    </div>';
        } else {
            return '<div id="pageLoad">
	        <a href="javascript:click_First_End(0)" class="click_btn" title="First Page">Frist</a>
	        <button id="prevPage" class="click_btn">Prev</button>
			<span id="number_span">
			</span>
	        <button id="nextPage" class="click_btn">Next</button>
	        <a href="javascript:click_First_End(1)" class="click_btn">Last</a>
	    </div>';
        }
    }

    static public function search() {
        return '<div id="load_search">
			<input  type="text" class="search_input " id="Loadsearch" name="Loadsearch">
			<button class="search_btn" onclick="click_search()">查询</button> 
		</div>';
    }
}

?>